package com.itany.netclass.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.netclass.constant.PageConstant;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.Comment;
import com.itany.netclass.entity.Resource;
import com.itany.netclass.entity.SearchForm;
import com.itany.netclass.exception.NoQuanXianException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.exception.ServiceException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CommentService;
import com.itany.netclass.util.ParameterUtil;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:2021/10/18 21:47
 * Version:1.0-SNAPSHOT
 */
@RequestMapping("/comment")
public class CommentController {
    @ResponseBody
    @RequestMapping("/findCommentByChapterId")
    public ResponseResult findCommentByChapterId(HttpServletRequest request, HttpServletResponse response){
       ResponseResult result=new ResponseResult();

        SearchForm commentSearchForm = new SearchForm();//刚进页面改用new新的一个查询的条件
        request.getSession().setAttribute("commentSearchForm",commentSearchForm);

        String chapterId = request.getParameter("chapterId");
        System.out.println("chapterId="+chapterId);
        request.getSession().setAttribute("chapterIdComment",chapterId);
        request.getSession().setAttribute("qianyiye",null);
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentService");
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        if(ParameterUtil.isNull(pageNo)){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize=PageConstant.PAGE_SIZE;
        }

        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<Comment> list=commentService.findCommentByChapterId(chapterId);
            PageInfo<Comment> pageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("commentList",pageInfo);
            request.getSession().setAttribute("Mrg;",null);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(pageInfo);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    @RequestMapping("/searchCommentByForm")
    public String searchCommentByForm(HttpServletRequest request, HttpServletResponse response){
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentService");
        String nickname = request.getParameter("nickname");
        System.out.println("nickname="+nickname);
        String context = request.getParameter("context");
        System.out.println("context="+context);
        String kaishitime=request.getParameter("kaishitime");
        System.out.println("kaishitime="+kaishitime);
        String jieshutime=request.getParameter("jieshutime");
        System.out.println("jieshutime="+jieshutime);
        String status=request.getParameter("course-resource-stauts-search");
        System.out.println("status="+status);
        String chapterId= (String) request.getSession().getAttribute("chapterIdComment");

        SearchForm commentSearchForm = new SearchForm();
        commentSearchForm.setContext(context);
        commentSearchForm.setCommentUserName(nickname);
        commentSearchForm.setKaishitime(kaishitime);
        commentSearchForm.setJiesutime(jieshutime);
        commentSearchForm.setStatus(status);
        request.getSession().setAttribute("commentSearchForm",commentSearchForm);//改用对象封装动态查询的条件



        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        if(ParameterUtil.isNull(pageNo)){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<Comment> list=commentService.searchCommentByForm(chapterId,context,nickname,kaishitime,jieshutime,status);
            PageInfo<Comment> pageInfo =new PageInfo<>(list);
            request.getSession().setAttribute("Mrg",null);
            request.getSession().setAttribute("commentList",pageInfo);
            request.getSession().setAttribute("qianyiye",null);
            return "/showComment.do";
        } catch (NullSringException e) {
            e.printStackTrace();
            request.getSession().setAttribute("commentList",new PageInfo<Comment>());
            request.getSession().setAttribute("Mrg",e.getMessage());
            return "/showComment.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showComment.do";
        }

    }
    @RequestMapping("/searchCommentByForm2")
    public String findResourceByForm2(HttpServletRequest request,HttpServletResponse response){

        //获取之前的查询的条件以保持页面状态
        SearchForm commentSearchForm= (SearchForm) request.getSession().getAttribute("commentSearchForm");

        String context=commentSearchForm.getContext();
        String nickname=commentSearchForm.getCommentUserName();
        String kaishitime=commentSearchForm.getKaishitime();
        String jieshutime=commentSearchForm.getJiesutime();
        String status = commentSearchForm.getStatus();

        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentService");
        String chapterId= (String) request.getSession().getAttribute("chapterIdComment");
        String pageNo=null;//响应后显示的页数
        String pageNo1 = request.getParameter("pageNo");//get传过来的页数

        //pageNo2在第一次查询所有时没有意义，作用是用来保证通过别的按钮返回当前页时留在当前页
        PageInfo<Resource> Info = (PageInfo<Resource>) request.getSession().getAttribute("commentList");
        String pageNo2="";
        if(Info!=null){
            pageNo2=""+Info.getPageNum();
        }

        //这个是表示在找到结果是如果数据改了，导致页面变少，自动跳转到前一页的页数
        String pageNo3 = (String) request.getSession().getAttribute("qianyiye");
        System.out.println("pageNo1="+pageNo1);
        System.out.println("pageNo2="+pageNo2);
        System.out.println("pageNo3="+pageNo3);


        //以下为使用哪一个作为当前页数的判断
        if(pageNo3!=null){
            System.out.println("用了前一页");
            pageNo=pageNo3;
        }else if(pageNo1!=null){
            System.out.println("用了get");
            pageNo=pageNo1;
        }else if(!ParameterUtil.isNull(pageNo2)){
            System.out.println("用了session");
            pageNo=pageNo2;
        }else {
            System.out.println("用了常量");
            pageNo=PageConstant.PAGE_KAISHI_NO;
        }

        String pageSize = request.getParameter("pageSize");
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        List<Comment> list= null;
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            list=commentService.searchCommentByForm(chapterId,context,nickname,kaishitime,jieshutime,status);
            PageInfo<Comment> pageInfo =new PageInfo<>(list);
            request.getSession().setAttribute("commentList",pageInfo);
            request.getSession().setAttribute("Mrg",null);
            request.getSession().setAttribute("qianyiye",null);
            return "/showComment.do";
        } catch (NullSringException e) {
            if(pageNo.equals(PageConstant.PAGE_KAISHI_NO)){
                //已经是第一页，表示为无满足条件的对象
                request.getSession().setAttribute("commentList",new PageInfo<>());
                request.getSession().setAttribute("Mrg","未查询到任何结果");
                return "/showComment.do";
            }else {
                //设置前一页的页码放到session中
                String pageNum = ""+(Info.getPageNum()-1);
                request.getSession().setAttribute("qianyiye",pageNum);
                return "/comment/searchCommentByForm2.do";
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showComment.do";

        }
    }
    @ResponseBody
    @RequestMapping("/yongCommentByComment")
    public  ResponseResult yongCommentByComment(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String commentId = request.getParameter("commentId");
        String status = request.getParameter("status");
        CommentService commentService= (CommentService) ObjectFactory.getObject("CommentService");
        try {
            commentService.yongCommentByComment(commentId,status);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NoQuanXianException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
